import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 10000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    if (store.getters.token) {
      config.headers['Token'] = store.getters.token
    }
    return config
  },
)
service.interceptors.response.use(
  function (res) {
    if (res.data.code === 20000 || res.data.code === 200) {
      return res.data;
    } else {
      this.$message.error(res.data.message);
      return Promise.reject(res.data);
    }
  },
  function (e) {
    console.log("请求错误：", e);
    this.$message.error("请求错误");
    return Promise.reject(e);
  }
);

export default service
